package com.sssoft.Yundian.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.DataDao;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class ForgetActivity extends Activity {
	private DataDao dao;
	private EditText et_info;
	private EditText et_infoo;
	Button button1;
	Button button2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget);

		button2 = (Button) findViewById(R.id.bt_Menu);
		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ForgetActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});

		dao = new DataDao(this);

		et_info = (EditText) findViewById(R.id.et_info);
		et_infoo = (EditText) findViewById(R.id.et_infoo);

		button1 = (Button) findViewById(R.id.button1);
		button1.setOnClickListener(new Button.OnClickListener() {

			// 验证，将文本账号密码转换成字符串形式
			@Override
			public void onClick(View view) {
				String name = et_info.getText().toString();

				String newpassword = et_infoo.getText().toString();

				String pattern = "^[1][0-9]{10}$";

				// 创建 Pattern 对象
				Pattern r = Pattern.compile(pattern);

				// 现在创建 matcher 对象
				Matcher m = r.matcher(name);
				if (m.find()) {
					if (dao.CheckIsDataAlreadyInDBorNot(name)) {

						dao.update(newpassword, name);

						Toast.makeText(ForgetActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
						Intent intent = new Intent(ForgetActivity.this, LoginActivity.class);
						startActivity(intent);
						finish();
					} 
				} else {
					Toast.makeText(ForgetActivity.this, "请输入正确的手机号!", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}
}



